<?php
require_once("constraint.php");
	
class EKStatistics{
	static function getStatistics($attri){
		$xmlfile = SERVERLOCATION."data/EKStatistics.xml";
		$doc = DOMDocument::load($xmlfile);
		$doc->validate();
		$statistics = $doc->getElementsByTagName("statistics")->item(0);
		return $statistics->getElementsByTagName($attri)->item(0)->nodeValue;
	}
	static function updateStatsitics($attri,$value){
		$xmlfile = SERVERLOCATION."data/EKStatistics.xml";
		$doc = DOMDocument::load($xmlfile);
		$doc->validate();
		$statistics = $doc->getElementsByTagName("statistics")->item(0);
		if($value == "updateTime"){
			$statistics->getElementsByTagName($attri)->item(0)->nodeValue = $value;
		} else {
			$temp = $statistics->getElementsByTagName($attri)->item(0)->nodeValue;
			$statistics->getElementsByTagName($attri)->item(0)->nodeValue = (int)$temp + $value;
		}
		$doc->save($xmlfile);
	}
	# Document statistics
	static function getDocumentLastId(){
		$value = EKStatistics::getStatistics("documentLastId");
		EKStatistics::updateStatsitics("documentLastId",1);
		return "_".$value;
	}
	static function incDocumentSumNum(){
		EKStatistics::updateStatsitics("documentSumNum",1);
	}
	static function decDocumentSumNum(){
		EKStatistics::updateStatsitics("documentSumNum",-1);
	}
	
	# ParagraphRelationship statistics
	static function getParagraphRelationshipLastId(){
		$value = EKStatistics::getStatistics("paragraphRelationshipLastId");
		EKStatistics::updateStatsitics("paragraphRelationshipLastId",1);
		return "_".$value;
	}
	static function incParagraphRelationshipSumNum(){
		EKStatistics::updateStatsitics("paragraphRelationshipSumNum",1);
	}
	static function decParagraphRelationshipSumNum(){
		EKStatistics::updateStatsitics("paragraphRelationshipSumNum",-1);
	}
	
	# Paragraph statistics
	static function getParagraphLastId(){
		$value = EKStatistics::getStatistics("paragraphLastId");
		EKStatistics::updateStatsitics("paragraphLastId",1);
		return "_".$value;
	}
	static function incParagraphSumNum(){
		EKStatistics::updateStatsitics("paragraphSumNum",1);
	}
	static function decParagraphSumNum(){
		EKStatistics::updateStatsitics("paragraphSumNum",-1);
	}
	
	# Talk statistics
	static function getTalkLastId(){
		$value = EKStatistics::getStatistics("talkLastId");
		EKStatistics::updateStatsitics("talkLastId",1);
		return "_".$value;
	}
	static function incTalkSumNum(){
		EKStatistics::updateStatsitics("talkSumNum",1);
	}
	static function decTalkSumNum(){
		EKStatistics::updateStatsitics("talkSumNum",-1);
	}
	
	# Rating statistics
	static function getRatingLastId(){
		$value = EKStatistics::getStatistics("ratingLastId");
		EKStatistics::updateStatsitics("ratingLastId",1);
		return "_".$value;
	}
	static function incRatingSumNum(){
		EKStatistics::updateStatsitics("ratingSumNum",1);
	}
	static function decRatingSumNum(){
		EKStatistics::updateStatsitics("ratingSumNum",-1);
	}
	
	# Updatetime statistics
	static function getUpdateTime(){
		return EKStatistics::getStatistics("updateTime");
	}
	static function setUpdateTime(){
		EKStatistics::updateStatsitics("updateTime",time());
	}
}
?>